using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace Programonica.Igramonica.PhysXPipeline
{
    [ContentTypeWriter]
    public class RagdollWriter : ContentTypeWriter<RagdollContent>
    {
        protected override void Write(ContentWriter output, RagdollContent value)
        {
            var shapes = value.Shapes;

            WriteShapes(output, shapes);

            var joints = value.Joints;

            WriteJoints(output, joints);
        }

        private static void WriteShapes(ContentWriter output, List<ShapeContent> shapes)
        {
            output.Write(shapes.Count);

            foreach (var shape in shapes)
            {
                var capsuleContent = shape as CapsuleContent;

                if (capsuleContent != null)
                {
                    output.Write(0);
                    output.Write(capsuleContent.Height);
                    output.Write(capsuleContent.Radius);
                }
                else
                {
                    output.Write(1);
                }
                output.Write(shape.Id);
                output.Write(shape.Mass);
                output.Write(shape.Name);
                output.Write(shape.Transform);
            }
        }

        private static void WriteJoints(ContentWriter output, List<JointContent> joints)
        {
            output.Write(joints.Count);

            foreach (var joint in joints)
            {
                output.Write(joint.Actor1);
                output.Write(joint.Actor2);
                output.Write(joint.Name);
            }
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return "Programonica.Igramonica.Physics.RagdollReader, "
                   + "Programonica.Igramonica.Physics, "
                   + "Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
        }
    }
}